<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: ReportController.php 197 2011-02-18 12:45:33Z beimuaihui $
 */


class Mall_AllreportController extends Baogg_Controller_Action {

	function init() 
	{		
		Baogg_Language::loadFile("user");
	}
	
	function listAction(){
		
		$params=$this->getRequest()->getParams();
		$period=5;
		$arr_year=array();
		$cur_year=(int)date("Y");
		$period=5;
		for($i=$cur_year;$i>$cur_year-$period;$i--){
			$arr_year[]=array($i,$i.'年');
		}

		if(isset($params['user_id'])){
			$url_param['user_id']=$params['user_id'];
		}else{
			$url_param['user_id']=$_SESSION['admin']['user']['user_id'];
		}
		if(isset($params['begin_time'])){
			$url_param['begin_time']=(int)$params['begin_time'];
		}else{
			$url_param['begin_time']=(int)date("Y");
		}
		if(isset($params['model_id'])){
			$url_param['model_id']=$params['model_id'];
		}
		$url_param['quarter']=0;
		

		$arr_url=$params;
		$arr_url['action']='data';
		$arr_url=array_merge($arr_url,$url_param);
		//echo '<pre>';print_r($arr_url);exit;
		//$arr_user_url=array("module"=>"system","controller"=>"user","action"=>"store");
		
		
		
		$this->view->url=Baogg_Controller_Url::generate($arr_url);
		$arr_url['action']='download';
		$this->view->download_url=Baogg_Controller_Url::generate($arr_url);
		

		//$this->view->user_url=Baogg_Controller_Url::generate($arr_user_url);
		//echo '<pre>';print_r($arr_user_url);var_dump(Baogg_Controller_Url::generate($arr_url));exit;
		//$this->view->user_id=$url_param['user_id'];
		
		$this->view->begin_time_data=Zend_Json::encode($arr_year);
		$this->view->begin_time=$url_param['begin_time'];
		//$this->view->arr_year=Zend_Json::encode($arr_year);
		$this->view->quarter_data=Zend_Json::encode(array_merge(array(array('0',BAOGG_LANGUAGE::get('msg_select_item'))),BAOGG_LANGUAGE::array2store('arr_quarter')));
		$this->view->quarter=$url_param['quarter'];
	}

	function dataAction(){
		
		UserInfo::updateScore();
		
		$where_perm=array();
		$s_user_group_role=Baogg_Global::get('s.admin.user_group_role');
		$s_group_id=$s_user_group_role['user_group_id'];		
		$User=new User();
		if(!$User->isAdmin()){
		    foreach((array)$s_group_id as $v){
		        if(!$v){
		            $where_perm[]='user_id='.((int)Baogg_Global::get('s.admin.user.user_id'));
		        }else{
		            $where_perm[]='user_group_id='.((int)$v);
		        }
		    }
		}
		$where_perm=implode(' or ',$where_perm);		
		//echo '<pre>';print_r($_SESSION);print_r($s_user_group_role);exit;
		
		
		$begin_time=$this->getRequest()->getParam("begin_time");
		$quarter=$this->getRequest()->getParam("quarter");
		$or_quarter=array();
		if(in_array($quarter,array('1','2','3','4'))){
			$quarter_month=Baogg_Date::quarter2month($quarter);
			
			foreach($quarter_month as $v){
				$or_quarter[]="begin_time like '{$begin_time}-".sprintf("%02d",(int)$v)."-%'";
			}
			$or_quarter=array(implode(' or ',$or_quarter));
		}else{
			$or_quarter=array("begin_time"=>$begin_time."-%");
		}
		//get top user group		
		$UserGroup=new UserGroup();
		/*$rs_parent=$UserGroup->getArray(array("user_group_pid"=>0,"state"=>1));
		$arr_parent=array_keys($rs_parent);
		$arr_group=array();
		$rs_group=$UserGroup->getList(array("user_group_pid"=>$arr_parent,"state"=>1),array("sort"=>"user_group_pid","dir"=>"asc"),null,array("user_group_id","user_group_name"));
		foreach((array)$rs_group as $v){
			$arr_group[$v['user_group_id']]=$v['user_group_name'];
		}
		unset($rs_group);*/
		
		
		
		$arr_user_name=array();  //store use name by user_id
		$arr_user_group_name=array(); //store user group name by user_id
		$Model=new Model();
		

		$arr_quarter=array();  //quanter static
		$arr_total=array('total'=>0); //total static
		$total_score=0;
		$arr_user=array(array(),array(),array(),array()); 
		$arr_user_num=array(array(),array(),array(),array());//user number per quarter,for table rowspan
		$arr_user_group_num=array(array(),array(),array(),array());//user number per quarter and top user group,for table rowspan
	 //report 
        $Menu=new Menu();
        $menu_item=$Menu->getByKey('user_report');

        	

	        
		$rs_model=$Model->getList(array("state"=>1,"main_table"=>"user"));	
		foreach((array)$rs_model as $schema){
			
			$arr_total[$schema['ext_table']]=0;			
			
			$ModelTable=new ModelTable((int)$schema['model_id']);
			
			$rs_model_table=$ModelTable->getShowList($or_quarter);
			
			foreach((array)$rs_model_table as $v){
			    $where_user=array("user_id"=>$v['user_id'])+($User->isAdmin()?array():array($where_perm));			    
				$rs_user=$User->getList($where_user);//getOne($v['user_id']);				
				//not exist user
				if(!isset($rs_user[0])){
					continue;
				}
				
				$js_menu=array(
	        		"id"=>'menu_'.$menu_item['menu_id'],
	        		"itemId"=>'tbmenu_'.$menu_item['menu_id'],
		        	"text"=>str_replace(array('-','|'),array('',''),$menu_item['menu_name']),
		        	"url"=> $menu_item['url']."user_id/".$rs_user[0]['user_id']."/menu_id/".$menu_item['menu_id']."/",
		        	"pid"=>'menu_'.$menu_item['menu_pid'],
	        		"icon"=>BAOGG_UPLOAD_URL."image/menu/".$menu_item['img_icon'],
	        	); 	
	
		        $json_menu=new Zend_Json_Expr("top.addTab(".Zend_Json::encode($js_menu).")");
		       // $icon='<a href=\'javascript:'.$json_menu.'\'><img  src="'.BAOGG_UPLOAD_URL."image/menu/".$menu_item['img_icon'].'" /></a>';
		        
				$arr_user_name[$rs_user[0]['user_id']]='<a href=\'javascript:'.$json_menu.'\'>'.$rs_user[0]['user_name'].'</a>';
			
				$user_group_pid=(int)$UserGroup->getTopGroupById($rs_user[0]['user_group_id']);
				$rs_user_group_name=$UserGroup->getList(array('user_group_id'=>array($rs_user[0]['user_group_id'],$user_group_pid)));
				foreach($rs_user_group_name as $v_user_group_name){
				    $arr_user_group_name[$v_user_group_name['user_group_id']]=@$v_user_group_name['user_group_name'];
				}
				$quarter= Baogg_Date::month2quarter(substr($v['begin_time'],5,2));
				
				@$arr_user[$quarter][$user_group_pid][$rs_user[0]['user_group_id']][$v['user_id']][$schema['ext_table']]+=$v['score'];
				@$arr_user[$quarter][$user_group_pid][$rs_user[0]['user_group_id']][$v['user_id']]['total']+=$v['score'];
				@$arr_user[$quarter][$user_group_pid][$rs_user[0]['user_group_id']][$v['user_id']]['hours']=sprintf("%.1f",@$arr_user[$quarter][$user_group_pid][$v['user_id']]['total']/6.0);
				
				@$arr_user_group_num[$quarter][$user_group_pid][$v['user_id']]=1;
				@$arr_user_num[$quarter][$v['user_id']]=1;
				
				@$arr_quarter[$quarter][$schema['ext_table']]+=$v['score'];
				@$arr_quarter[$quarter]['total']+=$v['score'];
				@$arr_quarter[$quarter]['hours']+=sprintf("%.1f",@$arr_quarter[$quarter]['total']/6.0);
				
				@$arr_total[$schema['ext_table']]+=$v['score'];				
				@$arr_total['total']+=$v['score'];
				@$arr_total['hours']=sprintf("%.1f",@$arr_total['total']/6.0);
				/*if($schema['ext_table']=='user_report'){
					echo '<pre>';
					print_r($v); 
					echo $user_group_pid;
					var_dump($quarter);
					echo substr($v['begin_time'],5,2);
					exit;
				}*/
			}
		}
		
		$this->view->error_reporting = 0;
		$this->view->users=$arr_user;
		$this->view->user_num=$arr_user_num;
		$this->view->user_group_num=$arr_user_group_num;
		$this->view->quarters=$arr_quarter;
		$this->view->totals=$arr_total;
		$this->view->quarter_name=array("一","二","三","四");
		$this->view->year=date("Y",strtotime($begin_time));

		$this->view->user_name=$arr_user_name;
		$this->view->user_group_name=$arr_user_group_name;
		
		/*echo '<pre>';
		print_r($arr_user);
		echo Baogg_Db::debugDb(Zend_Registry::get("db"));
		exit;*/
	}
	
	function downloadAction(){
	    //echo "<pre>";print_r($_SERVER);exit;
	    $url=$this->getRequest()->getParams();
	    $url['action']='data';
        
	    ob_start();
	    $this->dataAction();
	    $this->view->getEngine()->display($url['module'].'/'.$url['controller'].'_'.$url['action'].'.tpl');
	    //$this->view->getEngine()->display('mall/Allreport_data.tpl');
	    $html=ob_get_contents();
	    ob_end_clean();
	    //echo $html;exit;
	    Baogg_File::genPdfByContent($html,'AllReport');
	    Baogg_File::download(BAOGG_UPLOAD_DIR.'pdf/AllReport.pdf');
	    $this->_helper->viewRenderer->setNoRender ();	
	}
}
?>
